package guzb.cnblogs.classloader.third2wrapper;

import guzb.cnblogs.classloader.third.*;
import guzb.cnblogs.classloader.thirdapi.v2.DeviceBasicInfoV2;
import guzb.cnblogs.classloader.thirdapi.v2.DeviceFactoryV2;

import java.util.List;

/**
 * 第2个第三方Jar包的业务层封装配接口实现类
 *
 * @author 顾志兵
 * @mail ipiger@163.com
 * @since 2024-05-21
 */
public class DeviceFactoryV2Impl implements DeviceFactoryV2 {

    private SampleApi sampleApi = new SampleApi();

    @Override
    public DeviceBasicInfoV2 getDeviceInfo(String deviceNo) {
        SampleDevice sampleDevice = sampleApi.checkDevice(deviceNo);
        if (sampleDevice == null) {
            return null;
        }
        return toDeviceBasic(sampleDevice);
    }

    private DeviceBasicInfoV2 toDeviceBasic(SampleDevice sampleDevice) {
        DeviceBasicInfoV2 deviceBasic = new DeviceBasicInfoV2();

        deviceBasic.setDeviceNo(sampleDevice.getSid());
        deviceBasic.setStatus(sampleDevice.getStatus().name());
        deviceBasic.setRegion(sampleDevice.getRegion());
        deviceBasic.setUsage(sampleDevice.getUsage());

        List<SocketSlot> slots = sampleDevice.getSocketSlots();
        if (slots == null || slots.isEmpty()) {
            return deviceBasic;
        }

        // 计算接口槽位的数量、总接口数和监测类接口数
        deviceBasic.setSlotCount(slots.size());
        int totalSocketCount = 0;
        int monitorSocketCount = 0;
        for (SocketSlot slot : slots) {
            totalSocketCount += slot.getSocketCount();
            for (Socket socket : slot.getConnectedSockets()) {
                if (socket.getType() == SocketType.MONITOR) {
                    monitorSocketCount ++;
                }
            }
        }
        deviceBasic.setTotalSocketCount(totalSocketCount);
        deviceBasic.setMonitorSocketCount(monitorSocketCount);

        return deviceBasic;
    }
}
